package pay;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class Main {
    @Test
    public  void main()  {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            //// 2. 发起API调用（以创建当面付收款二维码为例）
            //AlipayTradePrecreateResponse response = Payment.FaceToFace()
            //        .preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
//String subject, String outTradeNo, String totalAmount, String returnUrl
            AlipayTradePagePayResponse response = Payment.Page().pay(
                    "云课堂支付测试",
                    "2234567890",
                    "5799.00",
                    "http://www.jd.com");
            System.out.println(response);

            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.body + "，" + response.getBody());
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi-sandbox.dl.alipaydev.com";
        config.signType = "RSA2";
        config.appId = "9021000153634265";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCujuSrGT7HET74erqUALKHJ0xxkGA70OjHvEjn/ijEfLTjK/ohngmlJVdJVjGew4FVEXwMiwxASGLOllwZWA/6dhaOhzobe+cHJ/Ajm+QOhvgq2uiySOHlafDDa4Yh9+RWD73zAqTwbV9UxT5PYBBhdwZubRyqghv5+JQXF1KjhHIFmW4s8y9Y7wGP6+zUTuFsOclLD1Km+8LgA8EwvNwpe2bQ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7xn7q2j/odGcUHci4wkuFEkhrmNjf2btYNwBMk4gKLAlEk+ETI5WeJ/LJXD5VKL2d/t1Ne+66h9PAOMOvcXhY4YAMUlcbCmurTlfhnAoGBAK48VgwVMXBcv5JJoB9h6qoSL8/JIEOfxEo591TGDuBujNuJymcmRZJgpraQNJ5rUObcQODvv0OW0wMFPy429E5z3FjMiUlbiRd3NgBqoG6n4MeRiOzS1pvTg0+1uOWwLAAvvs1BZ4da4mY2dUlraMmA/KXcn+YH+gPBD75xga7m";
        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
        //config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
        //config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
        //config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApPQIBKK6q92SJZ8s1GryvMeUIf0J7IgxUhO73mKEBHimPYa8UqaDySzJg5RsUyUQEmajtgDJ4zeyFmZ3RNzy/IiH6+UJzR+CBIy4f2jEsf/j9SgDGdJqasazRaQr3TwXEhXRC3TBdeNlQCO9AalmY9vwsIy2VE9WibXEyBT1p5hwYfdWf/SFYSbrVweO93opmEjYSmd7zdOrssy1UrMvmUCNYKN8EFJo2LRQCJpDHZwxi6h0Kg/6y2/BKO3HIYQMDHTV2QgSsS5tl/Ph7/dbGn2nxe3An1KGPkXDdTUEMxNX3x4PF4eI/Wrn8Ytf5XdpfzHqJ9t+DPfEgBNArCaOEwIDAQAB";
        //可设置异步通知接收服务地址（可选）
        //config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";
        return config;
    }
}